//q5.cpp
//This program takes a number and a pattern to set a new number by.
//
//Binyamin 4 Jan 2018
#include<iostream>

using namespace std;

const int INCORRECT_INDICES = -1;
const int INCORRECT_NUM = -2;

//num_length
//input: a number
//output: number length
int num_length(int num)
{
	int count;
	count =0;
	while(num>0)
	{
		count++;
		num/=10;
	}
	return count;
}
//digit_at
//input: a number and index(starts at 0)
//output: the digit on that index
int digit_at(int num, int index)
{
	int len = num_length(num);
	for(int i=0;i<( len - 1 - index);i++)
		num = num/10;
	return num%10;
}

//reform
//input: a number and a pattern.
//output: a reform number with that pattern
int reform(int num, int indices)
{
	int reformed_num;
	int num_len;
	int current_indice;
	int index;

	num_len = num_length (num);
	reformed_num=0;
	index = 1;

	if((num<0) || (num_length (num) >9))
		return INCORRECT_NUM;
	if(indices<0)
		return INCORRECT_INDICES;
	while(indices>0)
	{
		current_indice = indices%10;
		if(current_indice > num_len) return INCORRECT_INDICES;
		reformed_num += digit_at(num, current_indice)*index;
		index*=10;
		indices/=10;
	}
	return reformed_num;

}

//1. Prompt for a number and a pattern
//2. capture the number and pattern.
//3. reform the number
int main()
{
	int num, indices; //num, indices - input from the user.
	cout << "Enter a number and a reform pattern" << endl;
	cin >> num >> indices;
	cout <<"The new number is " << reform(num, indices) << endl;
	return 0;
}
//output
//Enter a number and a reform pattern
//4735 2010
//The new number is 3474
